/**
 * @author Kristian Mandrup
 * @date June 10, 2009
 * @version 0.1
 * 
 * m3gen :: ResolvedContentBuilder
 * 
 * Resolves content of a jsonObj or a branch and creates a resolvedContent object
 * 
 * 	var resolvedContent = {
		branch: true/false
		id: id,
		unique_id: unique_id
		state: state,
		icon: icon,
		title: title,
		languageData: languageData			
	}
 * 
 * Usage 
 * -----
 * var my_ResolvedContentBuilder = new ResolvedContentBuilder();
 * var resolvedJsonContent = my_ResolvedContentBuilder.buildFromJson(jsonObj);
 * 
 * var resolvedBranchContent = my_ResolvedContentBuilder.buildFromBranch(branchDefinition, branchRules);
 */

 
 /**
 * ResolvedContentBuilder constructor
 * Sets up ResolvedContentBuilder for use
 * 
 */
m3gen.ResolvedContentBuilder = function(createOptions) {
	if (arguments.length < 1 || m3gen.util.invalidOptions(createOptions))
		return;
	this.init(createOptions);		
}

m3gen.ResolvedContentBuilder.inherits(m3gen.Base);

m3gen.ResolvedContentBuilder.prototype.init = function(createOptions){	
	this.setClassName('ResolvedContentBuilder');		

	// call Parser superclass initializer 
	this.uber('init', createOptions);
}

/**
 * Public
 * A NodeObjBuilder musthave an execute function
 * 
 * Builds a nodeObj from resolved content.
 * If the resolved content contains
 * 
 * @param 	{Object} resolvedContent - contains content resolved from a jsonObj
 * 
 * @return 	{Object} nodeObj
 * 
 * TODO: nodeObj could be its own class with own constructor and functions?
 */

/*
	var resolvedContent = {
		branch: true/false
		id: id,
		unique_id: unique_id
		state: state,
		icon: icon,
		title: title,
		languageData: languageData			
	}
 */

/**
 * doExecute default empty wrapper
 * @param {Object} options
 * 
 * @return {NodeObject}	nodeObj	a simple nodeObj without children
 * 
 * nodeObj = {
 * 	attributes 
 *  state (for branch only)
 *  languageData (if languageOn only)
 *  data (if language not on) 
 */
m3gen.ResolvedContentBuilder.prototype.buildFromJson = function(options){
	return this.doBuildFromJson(options);
}


m3gen.ResolvedContentBuilder.prototype.doBuildFromJson = function (options) {	
	var ruleSet = null;
	var jsonObj = null;
	if (options) {
		ruleSet = this.getAnObj(options.ruleSet);
		jsonObj = this.getAnObj(options.jsonObj);
	}
	
	var activeRuleSet = ruleSet || this.getActiveRuleSet();
	var activeObj = jsonObj || this.getActiveObj();

	// {id: "...", unique_id: "..."}	
	var idObj = this.resolveId(activeRuleSet, activeObj);
	var uniqueId = this.resolveUniqueId(idObj);		

	var attributes = this.resolveAttributes(activeRuleSet, activeObj);

	var state = this.resolveState(activeRuleSet, activeObj);

	var title = this.resolveTitle(activeRuleSet, activeObj);

	var icon = this.resolveIcon(activeRuleSet, activeObj);

	var languageData = this.resolveRuleSetLanguages(activeRuleSet, activeObj);			
	
	// create resolvedContent object	
	var resolvedContent = {
			branch: false,
			id: idObj.id, 
			unique_id: uniqueId,
			attributes: attributes, 
			state: state, 
			languageData: languageData,
			data: {
				title: title,
				icon: icon
			}
	};
	
	if (this.getLanguageModeOn() && this.isValidObj(languageData)) {
		resolvedContent.languageData = languageData;
	}
	else {
		var data = {title: title};
		if (icon)
			data.icon = icon;		
		resolvedContent.data = data;
	}
	return resolvedContent;		
}

/**
 * Resolve branchNode content using branchDefinition and branchType rule
 * 
 * @param {Object} branchNode
 * @param {Object} branchTypeRule
 * 
 */

m3gen.ResolvedContentBuilder.prototype.buildFromBranch = function(branchDefinition, branchTypeRule){
	return this.doBuildFromBranch(branchDefinition, branchTypeRule);
}

m3gen.ResolvedContentBuilder.prototype.doBuildFromBranch = function (branchDefinition, branchTypeRule) {		
	var id = this.getBranchId(branchDefinition);
	var languageData = this.getBranchLanguageData(branchDefinition);	
	var title = this.getBranchTitle(branchDefinition);
	var btr_icon = null;
	var btr_state = null;
	var btr_lang_data = null;
	if (this.isValidObj(branchTypeRule)) {
		btr_icon = this.getBranchTypeRuleIcon(branchTypeRule);
		btr_state = this.getBranchTypeRuleState(branchTypeRule);
		btr_lang_data = this.getBranchTypeRuleLanguageData(branchTypeRule)
	}
	
	// create languageData
	var mergedLangData = null;
	if (this.isValidObj(languageData) || this.isValidObj(btr_lang_data)) {
		mergedLangData = this.mergeLanguageData(languageData, btr_lang_data);
	}
	
	// should use ruleSet defaults if no matching branchTypeRule
	var icon = this.getIconsPath(btr_icon);
	var state = this.getStateOfBranchRule(btr_state);
	
	var resolvedBranchContent = {
		branch: true,
		id: id,
		state: state,
	}

	if (this.getLanguageModeOn() && this.isValidObj(languageData)) {
		if (mergedLangData) {
			languageData = mergedLangData
		}		
		resolvedBranchContent.languageData = languageData;
	}
	else {
		var data = {title: title};
		if (this.isValidObj(icon))
			data.icon = icon;		
		resolvedBranchContent.data = data;
	}							
	return resolvedBranchContent;		
}